<?php
class PagesController extends AppController {
	var $name = 'Pages';
	var $uses = array();
    var $components = array('Email');

	public function display () {
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('page', 'subpage', 'title'));
		$this->render(join('/', $path));
	} // display

    public function axUpload ($prefix) {
        if (!$this->_checkLogged('A')) {
            return;
        }

        App::import('Vendor', 'Image', array('file'=>'image.class.php'));

        $json = $this->params['form'];
        $name = basename($_FILES['uploadImage']['tmp_name']);

        list($width, $height, $mime,) = @getimagesize($_FILES['Filedata']['tmp_name']);

        if ($prefix == 'aboutus') {
            $wdt = 618;
            $hgh = 558;
        } else if ($prefix == 'aboutus2') {
            $wdt = 202;
            $hgh = 226;
        } else {
            $wdt = 618;
            $hgh = 558;
        }

        $img = new Upload($_FILES['uploadImage']);

        $img->file_max_size      = 4096000;
        $img->allowed            = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
        $img->file_new_name_body = $prefix.'_'.$name;
        $img->image_min_width    = 64;
        $img->image_min_height   = 64;
        $img->image_x            = $wdt;
        $img->image_y            = $hgh;
        $img->image_ratio_crop   = true;
        $img->image_resize       = true;
        $img->Process(IMAGES.'cms'.DS);

        if ($img->processed) {
            $filename = $img->file_dst_name;
        } else {
            $json = array(
                'status'  => -1,
                'message' => __($img->error, true)
            );
        }
        $img->Clean();

        foreach (glob(IMAGES.'/cms/'.$prefix.'_*') as $f) {
            if (basename($f) != $filename) {
                @unlink($f);
            }
        }

        $this->set('json', array(
            'status'   => 1,
            'filename' => $filename
        ));
        $this->render(false, 'json');
        sleep(1);
    } // axUpload

    public function home () {
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js');
        $this->pageTitle = __('Home',true);
    } // home

    public function aboutus () {
        $files  = glob(IMAGES.'/cms/aboutus_*');
        $files2 = glob(IMAGES.'/cms/aboutus2_*');

        $filename  = (count($files) > 0) ? basename($files[0]) : '';
        $filename2 = (count($files2) > 0) ? basename($files2[0]) : '';

        $this->loadModel('Link');
        $links = $this->Link->find('all', array('order'=>'id'));

        $this->javascripts = am($this->javascripts, 'webtoolkit.aim');
        $this->pageTitle = __('About Us',true);
        $this->set('filename', $filename);
        $this->set('filename2', $filename2);
        $this->set('links', $links);
    } // aboutus

    public function contact () {
        $form = $this->params['form'];

        if ($form && count($form)>0) {
            $sitename = Configure::read('site.sitename');
            $domain   = str_replace('www.', '', env('HTTP_HOST'));
            $subject  = ($form['subject']) ? ' - '.$form['subject'] : '';
            $delivery = (Configure::read('debug') > 1) ? 'mail' : 'mail';
            $sendto   = 'hann@carelatex.com';

            if (isset($form['emailme']) && $form['emailme'] == 'Y') {
                $this->Email->to  = $form['email'];
                $this->Email->bcc = array($sendto);
            } else {
                $this->Email->to = $sendto;
            }

            $this->Email->replyTo  = $form['email'];
            $this->Email->from     = $sitename.' <noreply@'.$domain.'>';
            $this->Email->subject  = 'Contact Us'.$subject;
            $this->Email->template = 'contact';
            $this->Email->sendAs   = 'text';
            $this->Email->delivery = $delivery;

            $this->set($form);
            $this->Email->send();

            $json = array(
                'status' => true,
                'mesg'   => 'Contact us has been sent successfully.'
            );
            $this->response($json);
            return;
        }

        $this->pageTitle = __('Contact Us',true);
    } // contact

    public function cart () {
        $this->pageTitle = __('Your shopping cart',true);
        $this->set('cart', $this->Session->read('Cart'));
    } // cart

    public function updateCart ($id) {
        $cart = $this->Session->read('Cart');

        if (is_array($cart)) {
            $id = $this->params['form']['id'];

            foreach ($cart as $i=>$c) {
                if ($c['id'] == $id) {
                    $c['qty']   = $this->params['form']['qty'];
                    $c['total'] = $c['price'] * $this->params['form']['qty'];

                    $cart[$i] = $c;
                    $this->Session->write('Cart', $cart);
                }
            }
        } else {
            $this->Session->setFlash('<h4>'.__('Invalid update.',true).'</h4>', 'default', array('class'=>'boxError'));
        }

        $this->redirect('/products/shopping');
    } // updateCart

    public function deleteCart () {
        $cart = $this->Session->read('Cart');
        $page = empty($this->params['form']['page']) ? 1 : $this->params['form']['page'];
        $new  = array();

        if (is_array($cart)) {
            $id = $this->params['form']['id'];

            foreach ($cart as $c) {
                if ($c['id'] != $id) {
                    $new[] = $c;
                }
            }

            $this->Session->write('Cart', $new);
        } else {
            $this->Session->setFlash('<h4>'.__('Invalid delete.',true).'</h4>', 'default', array('class'=>'boxError'));
        }

        $this->redirect('/products/shopping/page:'.$page);
    } // deleteCart

    public function paymentMethod () {
        if (!$this->_checkLogged()) {
            return;
        }

        $info = $this->Session->read('Info');
        $info = is_array($info) ? $info : array();
        $info = array_merge($info, $this->params['form']);

        if ($info['country'] == 'Malaysia') {
            $info['shipping'] = 15;
        } else {
            $info['shipping'] = 80;
        }

        $this->Session->write('Info', $info);

        $this->pageTitle = __('Payment Method',true);
        $this->set('data', $info);
    } // paymentMethod

    public function confirm () {
        if (!$this->_checkLogged()) {
            return;
        }

        $info = $this->Session->read('Info');
        $info = is_array($info) ? $info : array();
        $info = array_merge($info, $this->params['form']);

        $this->Session->write('Info', $info);

        $this->pageTitle = __('Checkout Confirmation',true);
        $this->set('cart', $this->Session->read('Cart'));
        $this->set('data', $info);
    } // confirm
} // PagesController

?>